package main

import (
	"fmt"
	"math"
)

//定义变量自己有初始值
func variableZeroValue() {
	var a int
	var s string
	fmt.Printf("%d %q \n", a, s)
}

//定义变量设定初始值
func variableInitialValue() {
	var b, c int = 5, 6
	var s string = "sss"
	fmt.Printf("%d %d %q \n", b, c, s)
}

//自己类型推断, 省略变量类型
func variableTypeDeduction() {
	var a = "111"
	var b = 2
	fmt.Printf("%s %d", a, b)
}

//变量声明的简洁写法
func variableShorter() {
	a, b, c, s := 1, 2, true, "ssss"
	fmt.Println(a, b, c, s)
}

func consts() {
	const filename = "abc.txt"
	const a, b = 3, 4
	const (
		bookName  = "红楼梦"
		movieName = "肖申克的救赎"
	)

	//常量不用强转
	var c int = int(math.Sqrt(a*a + b*b))
	fmt.Printf("C: %d \n", c)

	//变量需要进行强转
	var e, f int = 3, 4
	var g = int(math.Sqrt(float64(e*e) + float64(f*f)))
	fmt.Printf("G: %d \n", g)
}

const version = "1.1.0"

//枚举也是使用常量定义的
func enums() {
	const (
		php = iota
		java
		golang
		cpp
	)

	const (
		b = 1 << (10 * iota)
		kb
		mb
		gb
		tb
		pb
	)

	fmt.Println(php, java, golang, cpp)
	fmt.Println(b, kb, mb, gb, tb, pb)
}

//定义包内变量,即全局变量,用 var() 减少 var
//包内部变量必须使用 var 来声明
var (
	aa = 11
	ss = "jj"
)

func main() {
	variableZeroValue()
	variableInitialValue()
	variableTypeDeduction()
	variableShorter()
	fmt.Println(aa, ss)
	enums()
	consts()
}
